package mail

import (
	"fmt"
	"gitee.com/aldrice/monitor/configs"
	. "gitee.com/aldrice/monitor/internal/common"
	"github.com/matcornic/hermes/v2"
)

type EmailType string

const (
	EmailTypeNotify EmailType = "服务器异常警告"
)

func getNotifyEmailBody(username, server string, detail AlarmItem) *hermes.Body {
	return &hermes.Body{
		Title:    "服务器异常警报",
		Greeting: "您好",
		Name:     username,
		Intros: []string{
			"我们检测到您名下的一台服务器出现了异常, 告警细则如下",
		},
		Actions: []hermes.Action{
			{
				Instructions: "可以点击此处快速进入您的个人控制台",
				Button: hermes.Button{
					Color: "#DC4D2F", // Optional action button color
					Text:  "进入控制台",
					Link: fmt.Sprintf("http://%s%s%s",
						configs.Config.FrontendAddress,
						configs.ObjectDetailURL,
						detail.ObjectID),
				},
			},
		},
		Table: hermes.Table{
			Data: [][]hermes.Entry{
				// List of rows
				{
					// Key is the column name, Value is the cell value
					// First object defines what columns will be displayed
					{Key: "项目键", Value: "监控对象"},
					{Key: "项目值", Value: server},
				},
				{
					{Key: "项目键", Value: "告警等级"},
					{Key: "项目值", Value: levelFormatter(detail.AlarmLevel)},
				},
				{
					{Key: "项目键", Value: "触发规则"},
					{Key: "项目值", Value: fmt.Sprintf("%s [%s , %s]", detail.RuleName, detail.MinLimit, detail.MaxLimit)},
				},
				{
					{Key: "项目键", Value: "突破数值"},
					{Key: "项目值", Value: detail.Value},
				},
				{
					{Key: "项目键", Value: "触发时间"},
					{Key: "项目值", Value: detail.Time},
				},
				{
					{Key: "项目键", Value: "触发指标"},
					{Key: "项目值", Value: detail.Metric},
				},
				{
					{Key: "项目键", Value: "聚合类型"},
					{Key: "项目值", Value: detail.Method},
				},
			},
			Columns: hermes.Columns{
				// Custom style for each rows
				CustomAlignment: map[string]string{},
			},
		},
		Signature: "来自",
		Outros: []string{
			"该邮件为系统后台自动发送, 请不要回复",
		},
	}
}

func levelFormatter(level int) string {
	switch level {
	case 1:
		return "提示 (级别 1)"
	case 2:
		return "严重 (级别 2)"
	case 3:
		return "致命 (级别 3)"
	default:
		return "未知 (出错)"
	}
}
